package com.szpx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szpx.entity.Order;
import com.szpx.entity.Vip;
import com.szpx.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author wgg
 * @version 1.0.0
 * @title OrderController
 * @create 2024/4/10 11:17
 * @description
 **/
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/selectOrderById/{id}")
    public String selectOrderById(@PathVariable("id") Integer id, Model model) {
        Order order = orderService.selectOrderById(id);
        if (order != null) {
            model.addAttribute("order", order);
            return "wg/wg_OrderDetail";
        } else {
            return null;
        }
    }

    @RequestMapping("/deleOrder/{id}")
    public String deleOrder(@PathVariable("id") Integer id) {
        int num = orderService.deleOrder(id);
        if (num > 0)
            return "redirect:/olist"; //重定向到用户列表请求
        else
            return null;
    }

     //修改
    @GetMapping("/toUpdateOrder/{id}")
    public String toUpdatePage(@PathVariable("id") Integer id, Model model) {
        Order order = orderService.selectOrderById(id);
        if (order != null) {
            model.addAttribute("order", order); //将查询出来的信息 传到修改页面
            return "wg/wg_OrderUpdate"; //逻辑视图
        } else {
            return null;
        }
    }

    /**
     * 修改影片信息
     */
    @RequestMapping("/updateOrder")
    public String updateOrder(Order order){
        int num = orderService.updateOrder(order);
        if (num > 0)
            return "redirect:/olist";
        else
            return null;
    }

    //增加订单
    @RequestMapping("/addOrder")
    public String addOrder(Order order) {
        int num = orderService.addOrder(order);
        if (num > 0)
            return "redirect:/olist";
        else
            return null;
    }

    //去往添加订单页面
    @RequestMapping("/toAddOrder")
    public String toAddOrder(HttpSession session) {

        return "wg/wg_OrderAdd";
    }



    //查询订单
    @GetMapping("/olist")
    public String getOList(
            @RequestParam(defaultValue = "1") int pno,
            @RequestParam(defaultValue = "3") int pagesize,
            Order order, Model model) {
        PageHelper.startPage(pno, pagesize); //开始分页
        List<Order> olist = orderService.getListByO(order);
        PageInfo<Order> page = new PageInfo<Order>(olist);
        model.addAttribute("olist",olist); //将查询到的数据放入model对象中
        model.addAttribute("page",page);
        model.addAttribute("oname",order.getOname());
        model.addAttribute("price",order.getPrice());
        model.addAttribute("pno",pno);
        return "wg/wg_OrderOlist";
    }

    //访问
   // @GetMapping("/model")
    //public String model(Model model) {
    //    return "model"; //逻辑视图
   // }

}

